/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elsimulatorjava;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;


/**
 *
 * @author plitter
 */
public class ElevatorPanel extends JComponent {
    private static final byte SELECT_WIDTH = 100;
    
    Elevator[] elevators;
    FloorButton[] buttons;
    
    short elevatorIdx;
    short floorCount;
    
    JPanel buttonPanel;
    JPanel emergencyButtonPanel;
    JPanel doorPanel;
    Door elevatorDoors;

    public ElevatorPanel(Elevator[] e, short nF, Dimension sizeOfPanel) throws IOException {
        elevators = e;
        elevatorIdx = 0;
        elevatorDoors = new Door(200, 400);
        doorPanel = new JPanel();
        doorPanel.add(elevatorDoors);
        floorCount = nF;
        
        ButtonGroup elevatorBG = new ButtonGroup();
        JRadioButton[] elevatorOptions = new JRadioButton[elevators.length];
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS));
        radioPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Elevator"),
            BorderFactory.createEmptyBorder(5,5,5,5)));
        for (short i = 0; i < elevators.length; i++){
            final short j = i;
            elevatorOptions[i] = new JRadioButton(String.valueOf(i));
            if (i == 0){
                elevatorOptions[i].setSelected(true);
            }
            elevatorOptions[i].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent ae) {
                        FloorButton.setElevatorIndex(j);
                        elevatorIdx = j;
                }
            });
            elevatorBG.add(elevatorOptions[i]);
            radioPanel.add(elevatorOptions[i]);
        }
        
        radioPanel.setPreferredSize(new Dimension(80, 50+23*elevators.length));
        JScrollPane radioScroll = new JScrollPane(radioPanel);
        radioScroll.setBounds(0, 0, SELECT_WIDTH, 530);
        
        FloorButton.setElevators(elevators);
        buttonPanel = new JPanel(new GridLayout(0, 4));
        buttons = new FloorButton[floorCount];
        if (floorCount<4) {
            buttonPanel.setLayout(new GridLayout(0, floorCount));
            for (int i = 0; i < floorCount; i++) {
                String buttonLabel = " "+ i;
                buttons[i] = new FloorButton(buttonLabel);
                buttonPanel.add(buttons[i]);
            }
        }
        else {
            buttonPanel.setLayout(new GridLayout(0,4));
            for (int i = 0; i < floorCount; i++) {
                if (i < 10) {
                    String buttonLabel = " "+ i;
                    buttons[i] = new FloorButton(buttonLabel);
                }
                else {
                    buttons[i] = new FloorButton(Integer.toString(i));
                }
                buttonPanel.add(buttons[i]);
            }
        }
        
        emergencyButtonPanel = new EmergencyPanel();
        
        add(radioScroll);
        add(elevatorDoors);
        add(buttonPanel);
        add(emergencyButtonPanel);
//        
        setLayout(null);
        
        //Dimensions of panels
        Dimension elevatorDoorsize = elevatorDoors.getPreferredSize();
        Dimension buttonsPanelSize = buttonPanel.getPreferredSize();
        Dimension emergencyButtonPanelSize = emergencyButtonPanel.getPreferredSize();
        
        int widthOfElevatorPanel = elevatorDoorsize.width+buttonsPanelSize.width+emergencyButtonPanelSize.width+10;
        int height;
        if (elevatorDoorsize.height > buttonsPanelSize.height) {
            height = elevatorDoorsize.height;
        }
        else {
            height = buttonsPanelSize.height;
        }
        
        Dimension elevatorPanel;// = additionDimension(elevatorDoorsize, buttonsPanelSize);
        if (elevatorDoorsize.height >= buttonsPanelSize.height) {
            elevatorPanel = new Dimension(elevatorDoorsize.width+
                    buttonsPanelSize.width+emergencyButtonPanelSize.width+10, elevatorDoorsize.height);     
        }
        else {
            elevatorPanel = new Dimension(elevatorDoorsize.width+
                    buttonsPanelSize.width+emergencyButtonPanelSize.width+10,buttonsPanelSize.height);
        }
        
        int widthStart = (sizeOfPanel.width-widthOfElevatorPanel)/2;
        
        // Elevator
        elevatorDoors.setBounds(widthStart+SELECT_WIDTH, getYCoord(sizeOfPanel.height, elevatorDoorsize.height), 
                elevatorDoorsize.width, elevatorDoorsize.height);
        widthStart += elevatorDoorsize.width;
        
        //Button
        buttonPanel.setBounds(widthStart+SELECT_WIDTH, getYCoord(sizeOfPanel.height, buttonsPanelSize.height), buttonsPanelSize.width, buttonsPanelSize.height);
        widthStart += buttonsPanelSize.width;
        
        // Emergency
        emergencyButtonPanel.setBounds(widthStart, 
                getYCoord(sizeOfPanel.height, emergencyButtonPanelSize.height), 
                emergencyButtonPanelSize.width, emergencyButtonPanelSize.height);
       
        
        
        //Emergency
        
        
        
        setSize(sizeOfPanel);
        
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        elevatorDoors.draw(g, elevatorDoors.getX(), elevatorDoors.getY());
        g.setColor(Color.black);
        g.drawString(String.valueOf(elevators[elevatorIdx].getPosition()), elevatorDoors.getX()+elevatorDoors.getElevatorWidth(), 60);
        if (elevators[elevatorIdx].getDirection() == Elevator.ElevatorDirection.UP){
            g.drawString("UP", elevatorDoors.getX()+elevatorDoors.getElevatorWidth()*2-50, 60);
        } else if (elevators[elevatorIdx].getDirection() == Elevator.ElevatorDirection.DOWN) {
            g.drawString("DOWN", elevatorDoors.getX()+elevatorDoors.getElevatorWidth()*2-50, 60);
        }
    }

    
    protected void updateElevatorPanel() throws IOException{
        this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Elevator " + elevatorIdx),
            BorderFactory.createEmptyBorder(5,5,5,5)));

        elevatorDoors.setOpening(elevators[elevatorIdx].getDoor());

        repaint();
    }
    

    
    public Dimension additionDimension(Dimension number_1, Dimension number_2) {
        Dimension sum = new Dimension(0, 0);
        sum.setSize(number_1.width+number_2.width, number_1.height+number_2.height);
        return sum;
    }

    private int getYCoord(int heightOfPanel, int heightOfComponent) {
        return (heightOfPanel-heightOfComponent)/2;
    }
}
